package com.weipai.springboot.module.dao.impl;

import com.weipai.springboot.model.ProductPro;
import com.weipai.springboot.model.ProductProperty;
import com.weipai.springboot.model.PropertyOption;
import com.weipai.springboot.module.dao.GoodsDao;
import com.weipai.springboot.module.dao.ProductPropertyDao;
import com.weipai.springboot.module.dao.PropertyOptionDao;
import com.weipai.springboot.module.mapper.GoodsMapper;
import com.weipai.springboot.module.mapper.ProductProMapper;
import com.weipai.springboot.module.mapper.ProductPropertyMapper;
import com.weipai.springboot.module.mapper.PropertyOptionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/24 14:33
 */
@Repository
public class ProductPropertyDaoImpl implements ProductPropertyDao{

    @Autowired
    ProductPropertyMapper productPropertyMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private PropertyOptionMapper propertyOptionMapper;
    @Autowired
    private ProductProMapper productProMapper;


    @Override
    public int deleteByPrimaryKey(String propertyId) {
        return productPropertyMapper.deleteByPrimaryKey(propertyId);
    }

    @Override
    public int insert(ProductProperty record) {
        return productPropertyMapper.insert(record);
    }

    //查询详情
    @Override
    public ProductProperty getPropertyById(String id) {
        return productPropertyMapper.getPropertyById(id);
    }

    //分页查询
    @Override
    public List<ProductProperty> selectAllByProductCategoryId(Integer start, Integer size, String productCategoryId) {
        return productPropertyMapper.selectAllByProductCategoryId(start,size,productCategoryId);
    }

    //分页的总数量
    @Override
    public Integer selectAllByProductCategoryIdAmount(String productCategoryId) {
        return productPropertyMapper.selectAllByProductCategoryIdAmount(productCategoryId);
    }

    //更改状态
    @Override
    public int updateStatus(String id, Integer status) {
        return productPropertyMapper.updateStatus(id, status);
    }

    //通过第三级的商品分类获取到属性与属性选项
    @Override
    public List<Object> getOptionByPropertyCategoryId(String id, String spuNumber, String type) {
        //获取此分类下的属性
        List<Map<String, Object>> propertyMap = productPropertyMapper.getPropertyByProductCategoryId(id);
        List<Object> list = new ArrayList<>();
        int size01 = propertyMap.size();
        if(size01>0){
            for(int i=0;i<size01;i++){
                getPropertyAndOption(propertyMap,i,spuNumber, list,type);
            }
        }
        return list;
    }

    @Override
    public int update(ProductProperty property) {
        return productPropertyMapper.updateByPrimaryKeySelective(property);
    }


    @Cacheable(value = "gld")
    public List<String> getAllProperty() {
        return productPropertyMapper.getAllProperty();
    }

    @Override
    public ProductProperty selectByPrimaryKey(String id) {
        return productPropertyMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ProductProperty> getProductPropertyByCategoryId(String id) {
        return productPropertyMapper.getProductPropertyByCategoryId(id);
    }

    @Override
    public Integer updatePropertyName(String id, String propertyName) {
        return productPropertyMapper.updatePropertyName(id, propertyName);
    }

    //通过产品的id来查询出此商品下面的所有属性和属性选项
    @Override
    public List<Object> getOptionByProductId(String productId, String spuNumber, String type) {
        //获取该产品关联的属性
        List<Map<String, Object>> propertyMap = productProMapper.getPropertyByProductId(productId);
        List<Object> list = new ArrayList<>();
        int size01 = propertyMap.size();
        if(size01>0){
            for(int i=0;i<size01;i++){
                getPropertyAndOption(propertyMap,i,spuNumber, list, type);
            }
        }
        return list;
    }

    //通过属性id删除属性选项
    @Override
    public void deleteOption(String id) {
        propertyOptionMapper.deleteByPropertyId(id);
    }


    /**
     * 获取所有的属性选项
     * @param propertyMap
     * @param i
     * @param spuNumber
     * @param list
     * @param type
     */
    private void getPropertyAndOption(List<Map<String, Object>> propertyMap, int i, String spuNumber, List<Object> list, String type) {
        Map<String, Object> map = new HashMap<>();
        String propertyId = propertyMap.get(i).get("propertyId").toString();
        map.put("propertyId",propertyId);
        map.put("propertyName",propertyMap.get(i).get("propertyName"));
        
        //获取此属性下的所有属性选项
        List<Map<String, Object>> optionMap = productPropertyMapper.getOptionByPropertyId(propertyId);
        
        map.put("option",optionMap);
        list.add(map);
        int size02 = optionMap.size();
        if(type.equals("true")){
            for(int y=0;y<size02;y++){
                
                Map<String, Object> map2 = optionMap.get(y);
                String optionId = map2.get("optionId").toString();
                int r = propertyOptionMapper.verifyIdUseBySpuNumberAndOption(spuNumber, optionId);
                if(r>0){
                    map2.put("isUse","true");
                }else{
                    map2.put("isUse","false");
                }
            }
        }
    }
}
